package cn.szabadsag.org.controller;

import cn.szabadsag.org.domain.Department;
import cn.szabadsag.org.query.DepartmentQuery;
import cn.szabadsag.org.service.IDepartmentService;
import cn.szabadsag.utils.AjaxResult;
import cn.szabadsag.utils.QueryResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

/**
 * 部门接口
 * @RestController这是一个返回json数据的类
 * @author yangcong
 * @date 2022年03月02日 11:49
 * @since
 */
//@Controller
@RestController
@RequestMapping("/department")
@Api(tags = "部门维护的接口")
//@CrossOrigin
public class DepartmentController {

    @Autowired
    private IDepartmentService departmentService;

    /**
     * 新增或修改
     */
    //@RequestMapping("/saveOrUptDepartment")
    //@ResponseBody
    @PutMapping
    public AjaxResult saveOrUptDepartment(@RequestBody Department department){
        try {
            if (department.getId() == null){
                //  新增
                departmentService.save(department);
            }else {
                //  修改
                departmentService.update(department);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }
    }

    /**
     * 删除
     */
    //@RequestMapping("/deleteDeptById")
    //@ResponseBody
    @DeleteMapping(value = "/{id}")
    public AjaxResult deleteDeptById(@PathVariable("id") Long id){
        try {
            departmentService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 批量删除
     */
    @PatchMapping
    public AjaxResult patchDel(@RequestBody Long[] ids){
        try {
            departmentService.patchDel(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setMessage(e.getMessage());
        }

    }

    /**
     * 查询单个
     */
    //@RequestMapping("/findDeptById")
    //@ResponseBody
    @GetMapping(value = "/{id}")
    public Department findDeptById(@PathVariable("id") Long id){
        return departmentService.loadById(id);
    }

    /**
     * 查询所有
     */
    //@RequestMapping("/findDepts")
    //@ResponseBody
    @GetMapping
    @ApiOperation(value = "查询部门的方法")
    public List<Department> findDepts(){
        return departmentService.loadAll();
    }

    /**
     * 高级查询
     * @param query
     * @return
     */
    @PostMapping
    public QueryResult<Department> queryPage(@RequestBody DepartmentQuery query){
        return  departmentService.queryPage(query);
    }


    @GetMapping("/deptTree")
    public List<Department> loadDeptTree(){
        return departmentService.loadDeptTree();
    }


















}
